package com.yang.jxj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.jxj.entity.Score;
import com.yang.jxj.entity.Students;
import com.yang.jxj.entity.Teachers;
import com.yang.jxj.entity.User;
import com.yang.jxj.service.impl.ScoreServiceImpl;
import com.yang.jxj.service.impl.StudentsServiceImpl;
import com.yang.jxj.service.impl.TeachersServiceImpl;
import com.yang.jxj.service.impl.UserServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



import java.util.HashMap;
import java.util.List;

import static com.yang.jxj.Utils.AesUtil.encrypt;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/api/Admin/")
public class UserController {

    @Autowired
    private UserServiceImpl userL;
    @Autowired
    private StudentsServiceImpl SuserL;
    @Autowired
    private TeachersServiceImpl TuserL;
    @Autowired
    private ScoreServiceImpl SscoreL;

    @PostMapping("/login")  //登录
    public HashMap<String,Object> login(@RequestBody User user){
        System.out.println(user);

        userL.Alogin(user);
        return userL.Alogin(user);
    }
    @PostMapping("/loginn")  //登录
    public HashMap<String,Object> loginn(@RequestBody User user){
        System.out.println(user);

        userL.loginById(user);
        return userL.loginById(user);
    }


    @GetMapping("/suser")   //分页查询
    public IPage<Students> findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam String studentId){
        Page<Students> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Students> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("student_id", studentId);
        objectQueryWrapper.orderByAsc("student_id");
        return SuserL.page(page,objectQueryWrapper);

    }

    @GetMapping("/tuser")   //辅导员分页查询
    public IPage<Teachers> findPage1(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam String teacherId){
        Page<Teachers> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Teachers> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("teacher_id", teacherId);
        objectQueryWrapper.orderByAsc("teacher_id");
        return TuserL.page(page,objectQueryWrapper);

    }

    @PostMapping("/saveS") //学生修改新增
    public HashMap<String,Object> saveS(@RequestBody Students students){
        HashMap<String,Object> map = new HashMap<>();
        User user = new User();
        Score score = new Score();
        score.setStudentId(students.getStudentId());
        user.setUserId(students.getStudentId());
        user.setUserRole(2);
        user.setPasswd(encrypt(String.valueOf(students.getStudentId())));
        if(String.valueOf(students.getStudentId()).length() != 10){
            map.put("code",1);
            map.put("msg","输入不正确");
            return map;
        }else{

            boolean save = SuserL.saveOrUpdate(students);
            boolean b = userL.saveOrUpdate(user);
            boolean b1 = SscoreL.saveOrUpdate(score);
            if(save && b && b1){map.put("code",0);
                map.put("msg","成功");
                return map;
            }else{
                map.put("code",1);
                map.put("msg","失败");
                return map;
            }

        }
    }

    @PostMapping("/saveT") //辅导员修改新增
    public HashMap<String,Object> saveS(@RequestBody Teachers teachers){
        HashMap<String,Object> map = new HashMap<>();
        User user = new User();
        user.setUserId(teachers.getTeacherId());
        user.setUserRole(1);
        user.setPasswd(encrypt(String.valueOf(teachers.getTeacherId())));
        if(String.valueOf(teachers.getTeacherId()).length() != 10){
            map.put("code",1);
            map.put("msg","输入不正确");
            return map;
        }else{
            boolean save = TuserL.saveOrUpdate(teachers);
            boolean b = userL.saveOrUpdate(user);
            if(save && b){map.put("code",0);
                map.put("msg","成功");
                return map;
            }else{
                map.put("code",1);
                map.put("msg","失败");
                return map;
            }

        }
    }
    @GetMapping("/delS/{id}") //删除学生
    public HashMap<String,Object> delS(@PathVariable Integer id){
        HashMap<String,Object> map = new HashMap<>();
        boolean b = SuserL.removeById(id);
        if(b){
            map.put("code",0);
            map.put("msg","删除成功");
            return map;
        }else {
            map.put("code",1);
            map.put("msg","删除失败");
            return map;
        }
    }
    
    @PostMapping("/delSs")//批量删除学生
    public HashMap<String,Object> delSs(@RequestBody List<Integer> ids){
        HashMap<String,Object> map = new HashMap<>();
        boolean b = SuserL.removeByIds(ids);
        if(b){
            map.put("code",0);
            map.put("msg","批量删除成功");
            return map;
        }else {
            map.put("code",1);
            map.put("msg","批量删除失败");
            return map;
        }
    }

    @GetMapping("/delT/{id}") //删除辅导员
    public HashMap<String,Object> delT(@PathVariable Integer id){
        HashMap<String,Object> map = new HashMap<>();
        boolean b = TuserL.removeById(id);
        if(b){
            map.put("code",0);
            map.put("msg","删除成功");
            return map;
        }else {
            map.put("code",1);
            map.put("msg","删除失败");
            return map;
        }
    }

    @PostMapping("/delTs")//批量删除学生
    public HashMap<String,Object> delTs(@RequestBody List<Integer> ids){
        HashMap<String,Object> map = new HashMap<>();
        boolean b = TuserL.removeByIds(ids);
        if(b){
            map.put("code",0);
            map.put("msg","批量删除成功");
            return map;
        }else {
            map.put("code",1);
            map.put("msg","批量删除失败");
            return map;
        }
    }
    @PostMapping("/updatepwd")//批量删除学生
    public HashMap<String,Object> update(@RequestBody User user) {
        HashMap<String, Object> map = new HashMap<>();
        if(user.getUserId() == null){
            map.put("code", 1);
            map.put("msg", "修改失败");
            return map;
        }
        user.setPasswd(encrypt(String.valueOf(user.getPasswd())));
        boolean b = userL.saveOrUpdate(user);
        if (b) {
            map.put("code", 0);
            map.put("msg", "修改成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "修改失败");
            return map;
        }
    }
//    @GetMapping("/exportS")
//    public void exportS(HttpServletResponse response){
//        try {
//            SuserL.export(response);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }



}
